
import datolib.*;

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class AppXmlReader {
	
	private File xmlFile;
	
	public AppXmlReader(File file) {
		this.xmlFile = file;
	}	
	
	public DatoliteApplication loadDatoliteApplication(String appId) {
	
		try {
	   		
	   		String applicationClass = "";
	   		String designDir = "";
	   		
	   		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       		DocumentBuilder db = dbf.newDocumentBuilder();
       		
       		Document xml = db.parse(this.xmlFile);
            Element applicationElement = xml.getDocumentElement();
       		
       		applicationClass = this.getAppXmlApplicationClass(applicationElement);
       		designDir = this.getAppXmlDesignDir(applicationElement);
       		
       		if (!this.validInfo(applicationClass, designDir)) {
       			return null;
       		}

    		
    		/* PlatformList platformList = this.platforms(applicationElement, appId, designDir);
    		
    		if (platformList.count() == 0) {
    			return null;
    		}*/
    		
       		DatoliteApplication datoliteApplication = 
       					new DatoliteApplication(appId, appId + "." + applicationClass, appId + "/" + designDir);
       		
       		this.platforms(applicationElement, datoliteApplication);
       		
       		return datoliteApplication;
       		
    	} catch (Exception e) {
    	e.printStackTrace();
    		return null;
    	}   
	}
	
	

	private String getAppXmlApplicationClass(Element applicationElement) {
	
		NodeList applicationNodes = applicationElement.getElementsByTagName("application-class");
       		
       	if (applicationNodes.getLength() == 0) {
       		return "";
       	}
       			
       	return applicationNodes.item(0).getTextContent();
       		
	}

	private String getAppXmlDesignDir(Element applicationElement) {

       	NodeList nodes = applicationElement.getElementsByTagName("design-dir");
       		
       	if (nodes.getLength() == 0) {
       		return "";
       	}
       		
       	return nodes.item(0).getTextContent();
       			
	}
	
	private void platforms(Element applicationElement, DatoliteApplication application) {
		
		NodeList nodes = applicationElement.getElementsByTagName("platforms").item(0).getChildNodes();
		
		
		for (int i = 0; i < nodes.getLength(); ++i) {
		
			
			Node node = nodes.item(i);
		
			if (node.getNodeType() != Node.ELEMENT_NODE) {
				continue;
			}
			
			String nodeName = node.getNodeName();
			
			Platform platform = Platform.NA;
			
			if (nodeName.equals("default")) {
				platform = Platform.DEFAULT;
			} else if (nodeName.equals("desktop")) {
				platform = Platform.DESKTOP;
			} else if (nodeName.equals("tablet")) {
				platform = Platform.TABLET;
			} else if (nodeName.equals("mobile")) {
				platform = Platform.MOBILE;
			}
 			
			
			this.loadPlatform(node, platform, application);
			
		}
		
	}
	
	private void loadPlatform(Node node, Platform platform, DatoliteApplication application) {
		
			
		NodeList childNodes = node.getChildNodes();
			
		String gui = "";
		String code = "";
			
		for (int j = 0; j < childNodes.getLength(); ++j) {
				
			Node childNode = childNodes.item(j);
			
			if (childNode.getNodeType() != Node.ELEMENT_NODE) {
				continue;
			}
				
				
			String childNodeName = childNode.getNodeName();
			
			if (childNodeName.equals("gui")) {
				gui = childNode.getTextContent();
			} else if (childNodeName.equals("code")) {
				code = childNode.getTextContent();
			}

		}
		
		application.install(platform, gui, code);

	}
	
	private boolean validInfo(String applicationClass, String designDir) {

		if (applicationClass.equals("")) 
			return false;
		if (designDir.equals(""))
			return false;
			
		return true;
	}
	
}
